﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif
using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Enum;
using Kugar.MVC.MobileUI.WeUI.Helpers;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons
{
    public class PageCommon_ModalWindow : ContainerBase
    {
        private string _title = "";
        private string _contentHeight = "450px";
        private List<ButtonItemBase> _lst=new List<ButtonItemBase>();
        private bool _hideOnClickMask = false;

        public PageCommon_ModalWindow(ViewContext viewContext) : base(viewContext)
        {
            this.Css("weui-dialog");
        }

        public PageCommon_ModalWindow Title(string title)
        {
            _title = title;

            return this;
        }

        public PageCommon_ModalWindow HideOnClickMask(bool autoHide = true)
        {
            _hideOnClickMask = autoHide;

            return this;

        }

        public PageCommon_ModalWindow AddButton(Action<ButtonItemBase> configFunc)
        {
            var config=new ButtonItemBase();

            config.Class("weui-dialog__btn");

            configFunc(config);
            
            _lst.Add(config);

            return this;
        }

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("</div> <!-- end  dialog__bd-->");

            if (_lst.HasData())
            {
                ViewContext.Writer.WriteLine("<div class=\"weui-dialog__ft\">");

                foreach (var config in _lst)
                {
                    if (config.getType== ButtonTypeEnum.Submit)
                    {
                        config.OnClick("WebUIJS.Submit(this);");
                    }

                    var style=Utility.ButtonStyleToStr(config._styleType);

                    config.Class(style);

                    ViewContext.Writer.WriteLine($"<a href=\"javascript:;\" {config.Attributes} {(config.getOnClickScript.IsNotNullOrEmpty() ? $"onclick =\"{config.getOnClickScript}\"" : "")} >{config.getText}</a>");
                }
                
                ViewContext.Writer.WriteLine("</div> <!-- end  dialog__ft-->");
            }

            new PageCommon_ServerMsg(ViewContext).Render();

            ViewContext.Writer.WriteLine("</div> <!-- end  weui-dialog -->");

            

            ViewContext.ViewBag.IsInModal = false;
        }

        internal override ContainerBase RenderInternal()
        {

            ViewContext.ViewBag.IsInModal = true;

            var writer = ViewContext.Writer;

            this.Data("data-hideoncilckmask", _hideOnClickMask.ToStringEx());

            writer.WriteLine($"<div {_attrs}>");

            if (!string.IsNullOrWhiteSpace(_title))
            {
                writer.WriteLine($"<div class=\"weui-dialog__hd\">");

                writer.WriteLine($"<strong class=\"weui-dialog__title\">{_title}</strong>");

                writer.WriteLine("</div> <!-- end weui-dialog__hd -->");                
            }



            writer.WriteLine($"<div class=\"weui-dialog__bd\">");


            return this;
        }

        
    }
}